﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.BackstageManagement.Views;
using MyVoteMVC.Service;
using MyVoteMVC.Common;
using System.IO;
using MyVoteMVC.Domain;
using System.Drawing;
using MyVoteMVC.BackstageManagement.Extensions;

namespace MyVoteMVC.BackstageManagement.Controllers
{
    public class ProjectManagementController : IProjectManagementController
    {
        IProjectManagementView view;
        IProjectService projectService;
        public ProjectManagementController(IProjectManagementView view)
        {
            this.view = view;
            projectService = Factory.GetInstance<IProjectService>();
        }

        #region IProjectManagementController Members

        public void GetProjects()
        {
            view.GetProjectsComplete(projectService.GetAllProjects());
        }

        public void CreateProject(
            string projectYear, 
            string projectAddress,
            string projectName, 
            string projectType, 
            string projectAwards, 
            string projectDescription,
            byte[] projectImage)
        {

            //check is null
            if (string.IsNullOrWhiteSpace(projectYear))
            {
                view.OnSubmitError("年代不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(projectAddress))
            {
                view.OnSubmitError("地址不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(projectName))
            {
                view.OnSubmitError("名称不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(projectType))
            {
                view.OnSubmitError("类型不为空");
                return;
            }
            //check length
            if (Encoding.Default.GetByteCount(projectType) > 50)
            {
                view.OnSubmitError("类型过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectYear) > 12)
            {
                view.OnSubmitError("年份过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectAddress) > 2000)
            {
                view.OnSubmitError("地址过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectAwards) > 2000)
            {
                view.OnSubmitError("获奖过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectDescription) > 2000)
            {
                view.OnSubmitError("描述过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectName) > 300)
            {
                view.OnSubmitError("名称过长");
                return;
            }

            try
            {
                projectService.CreateProjects(
                    projectYear,
                    projectAddress,
                    projectName,
                    projectType,
                    projectAwards,
                    projectDescription,
                    projectImage,
                    DomainExtension.Thumbnail(projectImage)
                    );
                view.CreateProjectComplete("创建成功");
            }
            catch
            {
                view.OnSubmitError("创建失败");
            }
        }

        public void EditProject(
            string projectID,
            string projectYear,
            string projectAddress,
            string projectName,
            string projectType,
            string projectAwards,
            string projectDescription,
            byte[] projectImage)
        {
            //check is null
            if (string.IsNullOrWhiteSpace(projectYear))
            {
                view.OnSubmitError("年代不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(projectAddress))
            {
                view.OnSubmitError("地址不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(projectName))
            {
                view.OnSubmitError("名称不为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(projectType))
            {
                view.OnSubmitError("类型不为空");
                return;
            }
            //check length
            if (Encoding.Default.GetByteCount(projectType) > 50)
            {
                view.OnSubmitError("类型过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectYear) > 12)
            {
                view.OnSubmitError("年份过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectAddress) > 2000)
            {
                view.OnSubmitError("地址过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectAwards) > 2000)
            {
                view.OnSubmitError("获奖过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectDescription) > 2000)
            {
                view.OnSubmitError("描述过长");
                return;
            }
            if (Encoding.Default.GetByteCount(projectName) > 300)
            {
                view.OnSubmitError("名称过长");
                return;
            }
            try
            {
                projectService.EditProjects(
                    projectID,
                    projectYear,
                    projectAddress,
                    projectName,
                    projectType,
                    projectAwards,
                    projectDescription);

                byte[] img = projectImage;
                if (img != null)
                {
                    projectService.ModifyProjectImage(projectID, img, DomainExtension.Thumbnail(img));
                }

                view.EditProjectComplete("修改成功");
            }
            catch
            {
                view.OnSubmitError("修改失败");
            }
        }

        public void DeleteProject(string projectID)
        {
            try
            {
                projectService.DeleteProjects(projectID);
                view.DeleteProjectComplete("删除成功");
            }
            catch
            {
                view.OnSubmitError("删除失败");
            }
        }

        #endregion

         private byte[] ReadFile(string fileName)
        {
            bool isBroken = false;
            try
            {
                Bitmap bmp = new Bitmap(fileName);
                if (bmp == null)
                {
                    //图片损坏
                    isBroken = true;
                }
            }
            catch //(Exception ex)
            {
                //图片损坏
                isBroken = true;
            }

            if (isBroken)
                return null;

            FileStream pFileStream = null;
            byte[] pReadByte = null;

            try
            {
                pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(pFileStream);
                r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开
                pReadByte = r.ReadBytes((int)r.BaseStream.Length);
                return pReadByte;
            }
            catch
            {
                return pReadByte;
            }
            finally
            {
                if (pFileStream != null)
                    pFileStream.Close();
            }
        }

         #region IProjectManagementController Members


         public void GetProject(string projectID)
         {
             view.GetProjectComplete(projectService.GetProjectById(projectID));
         }

         public void GetProjectsByType(string type)
         {
             view.GetProjectsByTypeComplete(projectService.GetProjectsByType(type));
         }

         public void GetProjectsByYear(string year)
         {
             view.GetProjectsByYearComplete(projectService.GetProjectsByYear(year));
         }

         public void GetProjectTypes()
         {
             //需要一个方法
             view.GetProjectTypesComplete(projectService.GetALLType());
         }

         public void GetProjectsYear()
         {
             //需要一个方法
             view.GetProjectsYearComplete(projectService.GetALLYear());
         }

         #endregion


         public void DeleteProject(List<VT_Projects> projects)
         {
             try
             {
                 projectService.DeleteProjects(projects);
                 view.DeleteProjectComplete("成功删除");
             }
             catch
             {
                 view.OnSubmitError("删除失败");
             }
         }


         public void GetProjects(string type, string year, string keyword)
         {
             view.GetProjectsComplete(projectService.FindAllProject(year, type, keyword));
         }
    }
    
}
